package achi.community.irla.beans;

import achi.community.irla.database.DatabaseImpl;
//import achi.community.irla.entities.Friends;
import achi.community.irla.entities.Achievements;
import achi.community.irla.entities.Achievements_comments;
import achi.community.irla.entities.Achievements_completed;
import achi.community.irla.entities.Achievements_suggested;
import achi.community.irla.entities.Achievements_target;
import achi.community.irla.entities.Messages;
import achi.community.irla.entities.Users;
import java.io.Serializable;
import java.util.LinkedList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.event.ActionEvent;

/**
 * AddControlBean:
 * Adds animals to the Database from manageShop.xhtml
 */
@ManagedBean
@RequestScoped
public class AddControlBean implements Serializable {

    @EJB
    DatabaseImpl db;
    @ManagedProperty(value = "#{addBackBean}")
    AddBackBean backingBean;

    public AddBackBean getBackingBean() {
        return backingBean;
    }

    public void setBackingBean(AddBackBean backingBean) {
        this.backingBean = backingBean;
    }

    public String addUser() {
        String firstname = backingBean.getFirstname();
        String lastname = backingBean.getLastname();
        String email = backingBean.getEmail();
        String username = backingBean.getUsername();
        String password = backingBean.getPassword();
        String avatar = backingBean.getAvatar();

        Users user = new Users(firstname, lastname, email, username, password, avatar);
        db.addUser(user);
        return "database";
    }

    public String addMessage() {
        String title = backingBean.getTitle();
        String time = backingBean.getTime();
        String content = backingBean.getContent();
        Long user_id = Long.parseLong(backingBean.getUserID());

        Messages msg = new Messages(title, time, content);
        db.addMessage(msg, user_id);

        return "page";
    }

    public String addFriend() {
        Long userID = Long.parseLong(backingBean.getUser());
        Long friendID = Long.parseLong(backingBean.getFriend());
        db.addFriend(userID, friendID);

        return "page";
    }

    public String addAchievement() {
        String title_sv = backingBean.getTitle_sv();
        String title_en = backingBean.getTitle_en();
        String desc_sv = backingBean.getDesc_sv();
        String desc_en = backingBean.getDesc_en();
        String category = backingBean.getCategory();
        String sub_category = backingBean.getSub_category();
        String image = backingBean.getImage();
        int points = backingBean.getPoints();
        String date_created = backingBean.getDate_created();

        Achievements achi = new Achievements(title_sv, title_en, desc_sv, desc_en, category, sub_category, image, points);
        achi.setDate(date_created);
        db.addAchievement(achi);

        return "page";
    }

    public String addAchievement_comment() {
        Long userID = Long.parseLong(backingBean.getComments_userID());
        Long achiID = Long.parseLong(backingBean.getComments_achiID());
        Long commenterID = Long.parseLong(backingBean.getCommenterID());
       // String date_created = backingBean.getDate_created();
        String message = backingBean.getComments_message();

        Achievements_comments achi = new Achievements_comments(commenterID, message);
       // achi.setDate_created(date_created);
        db.addAchievement_comment(achi, userID, achiID);

        return "page";
    }

    public String addAchievement_completed() {    
        Long userID = Long.parseLong(backingBean.getCompleted_userID());
        Long achiID = Long.parseLong(backingBean.getCompleted_achiID());
        String description = backingBean.getDescription();
        String date_completed = backingBean.getDate_completed();
        String pos = backingBean.getCompleted_pos();

        Achievements_completed achi = new Achievements_completed(description, date_completed, new LinkedList());
        achi.setPos(pos);
        db.addAchievement_completed(achi, userID, achiID);

        return "page";
    }

    public String addAchievement_suggested() {
        Long userID = Long.parseLong(backingBean.getSuggested_userID());
        Long achiID = Long.parseLong(backingBean.getSuggested_achiID());
        Long suggesterID = Long.parseLong(backingBean.getSuggesterID());
        String suggest_message = backingBean.getSuggest_message();

        Achievements_suggested achi = new Achievements_suggested(suggesterID, suggest_message);
       // achi.setDate_created(date_created);
        db.addAchievement_suggested(achi, userID, achiID);

        return "page";
    }

    public String addAchievement_target() {
        Long userID = Long.parseLong(backingBean.getTarget_userID());
        Long achiID = Long.parseLong(backingBean.getTarget_achiID());
        String pos = backingBean.getTarget_pos();
        Achievements achi = db.getAchievement(achiID);

        //Achievements_target achi = new Achievements_target();
       // achi.setDate_created(date_created);
        //db.addAchievement_target(userID, col, achi);
        db.updateTarget(userID, pos, achi);
        return "page";
    }
    
    public void removeUser(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeUser(id);
    }

    public void removeMessage(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeMessage(id);
    }

    public void removeFriend(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeFriend(id);
    }

    public void removeAchievement(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeAchievement(id);
    }

    public void removeAchievement_comment(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeAchievement_comment(id);
    }
    
    public void removeAchievement_completed(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeAchievement_completed(id);
    }

    public void removeAchievement_suggested(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeAchievement_suggested(id);
    }

    public void removeAchievement_target(ActionEvent evt) {
        HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
        Long id = Long.parseLong(b.getLabel());
        db.removeAchievement_target(id);
    }
    /**
    public void removeUMessage(ActionEvent evt) {
    HtmlCommandButton b = (HtmlCommandButton)evt.getSource();
    int id = Integer.parseInt(b.getLabel());
    db.removeUMessage(id);
    }
     */
}
